package com.polaris.live.ui.live.fragment.base

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.bumptech.glide.GenericTransitionOptions
import com.bumptech.glide.Glide
import com.polaris.live.R
import com.polaris.live.ui.live.bean.DragBean
import com.polaris.live.ui.live.fragment.base.pager.BaseRecyclerPagerAdapter
import com.polaris.live.utils.image.loadImage
import com.polaris.live.utils.image.normal
import jp.wasabeef.glide.transformations.BlurTransformation

/**
 * drag适配器
 *
 * @author Created by lucas on 2023/11/8 10:00
 * @since 1.0.0
 */
class DragAdapter<D : DragBean>(context: Context) :
    BaseRecyclerPagerAdapter<D, DragAdapter.ViewHolder>(context) {

    private var mPosition = 0
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(inflate(R.layout.live_item_drag))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        if (mPosition == position) {
            holder.itemView.visibility = View.GONE
        } else {
            holder.itemView.visibility = View.VISIBLE
        }

        //todo 封面可能为空，最好能够有默认图
        Glide.with(context)
            .loadImage(get(position).coverUrl)
            .transition(GenericTransitionOptions.withNoTransition())
            .transform(BlurTransformation(25, 4))
            .normal()
            .into(holder.mIvImage)
    }

    fun showView() {
        mPosition = -1
        notifyDataSetChanged()
    }

    fun setPosition(position: Int) {
        mPosition = position
    }

    fun hideView(position: Int) {
        mPosition = position
        notifyDataSetChanged()
    }

    class ViewHolder(view: View) : ViewPageHolder(view) {

        val mIvImage: ImageView

        init {
            mIvImage = view.findViewById(R.id.image)
        }
    }
}